package cn.faster.sys.service.impl;

import cn.faster.sys.entity.SysMenu;
import cn.faster.sys.entity.SysRole;
import cn.faster.sys.entity.SysRoleMenuCenter;
import cn.faster.sys.mapper.SysRoleMapper;
import cn.faster.sys.service.SysMenuService;
import cn.faster.sys.service.SysRoleMenuCenterService;
import cn.faster.sys.service.SysRoleService;
import cn.faster.sys.vo.SysRoleVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author xiaoCoder
 * @since 2021-06-10
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {


    private final SysRoleMenuCenterService sysRoleMenuCenterService;

    private final SysRoleMapper sysRoleMapper;

    /**
	* 根据id查询Role
	*/
    @Override
    public SysRole getRoleById(Integer id) throws Exception {
        return sysRoleMapper.getRoleById(id);
    }

    /**
	* 查询Role列表
	*/
    @Override
    public List<SysRole> listRole(Map<String, Object> map) throws Exception {
        return sysRoleMapper.listRole(map);
    }



    /**
	* 新增Role
	*/
    @Override
    @Transactional
    public Integer addRole(SysRoleVO sysRoleVO) throws Exception {
        sysRoleVO.setDelFlag(0);
        log.info(">>>新增 Role 开始 :{}>>>", JSONObject.toJSONString(sysRoleVO));
        // 新增 role 信息入库
        Integer integer = sysRoleMapper.insert(sysRoleVO);
        log.info(">>>新增 Role 后数据信息 :{}>>>", JSONObject.toJSONString(sysRoleVO));
        // 获取到 menu 信息
        List<SysMenu> sysMenus = sysRoleVO.getSysMenus();
        // 入库中间表
        for (SysMenu sysMenu : sysMenus) {
            // 获取数据信息
            SysRoleMenuCenter sysRoleMenu = new SysRoleMenuCenter();
            sysRoleMenu.setMId(sysMenu.getId());
            sysRoleMenu.setRId(sysRoleVO.getId());
            sysRoleMenuCenterService.addRoleMenuCenter(sysRoleMenu);
        }
        log.info(">>>新增 Role 完成>>>");

        return integer;
    }

    /**
	* 更新Role
	*/
    @Override
    @Transactional
    public Integer updateRole(SysRole sysRole) throws Exception {
        return sysRoleMapper.updateRole(sysRole);
    }

   	/**
	* 根据id删除Role
	*/
    @Override
    @Transactional
	public Integer deleteRoleById(Integer id) throws Exception {
        return sysRoleMapper.deleteRoleById(id);
    }

	/**
	* 根据id逻辑删除Role
	*/
    @Override
    @Transactional
	public Integer removeRoleById(Integer id) throws Exception {
        return sysRoleMapper.removeRoleById(id);
    }
}
